package com;

public class YesterDate {
    public static String getYesterday(int year, int month, int day) {
        if (day > 1) {
            day--;
        }
        else {
            if (month == 1) {
                year--;
                month = 12;
                day = 31;
            } else if (month == 3) {
                month--;
                day = isLeapYear(year) ? 29 : 28;
            } else {
                month--;
                switch (month) {
                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        day = 31;
                        break;
                    case 4: case 6: case 9: case 11:
                        day = 30;
                        break;
                    case 2:
                        day = isLeapYear(year) ? 29 : 28;
                        break;
                    default: // 添加 default 分支
                        throw new IllegalArgumentException("Invalid month: " + month);
                }
            }
        }
        return formatDate(year, month, day);
    }

    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    private static String formatDate(int year, int month, int day) {
        return String.format("%d-%02d-%02d", year, month, day);
    }
}
